package com.ruoyi.ucar.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.ucar.boreq.TotalCountBoReq;
import com.ruoyi.ucar.boresp.OrderTotalCountDataBo;
import com.ruoyi.ucar.boresp.ProductTotalCountDataBo;
import com.ruoyi.ucar.domain.TOrder;
import com.ruoyi.ucar.domain.po.*;
import com.ruoyi.ucar.moreq.OrderDetailQueryMoReq;
import com.ruoyi.ucar.moreq.OrderListProxyQueryMoReq;
import com.ruoyi.ucar.moreq.OrderListQueryMoReq;
import com.ruoyi.ucar.moreq.OrderSignQueryMoReq;
import com.ruoyi.ucar.util.SqlProviderUtil;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.HashMap;
import java.util.List;

/**
 * 订单主Mapper接口
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Mapper
public interface TOrderMapper extends BaseMapper<TOrder>
{

    @SelectProvider(type = SqlProviderUtil.class,method = "createSqlOfQueryOrderAndProductByCond")
    List<QueryOrderAndProductPo> queryOrderAndProductByCond(HashMap<String,Object> map);

    @SelectProvider(type = SqlProviderUtil.class,method = "createSqlOfMobileQueryOrderByCond")
    List<OrderMobileQueryListPo> mobileQueryOrderByCond(HashMap<String,Object> map);

    @SelectProvider(type = SqlProviderUtil.class,method = "createSqlOfQueryOrderCustomByCond")
    List<QueryOrderAndCustomerPo> queryOrderAndCustomerByCond(HashMap<String,Object> map);


    /**
     * 查询订单主
     * 
     * @param id 订单主主键
     * @return 订单主
     */
    public TOrder selectTOrderById(Long id);

    /**
     * 查询订单主列表
     * 
     * @param tOrder 订单主
     * @return 订单主集合
     */
    public List<TOrder> selectTOrderList(TOrder tOrder);

    /**
     * 新增订单主
     * 
     * @param tOrder 订单主
     * @return 结果
     */
    public int insertTOrder(TOrder tOrder);

    /**
     * 修改订单主
     * 
     * @param tOrder 订单主
     * @return 结果
     */
    public int updateTOrder(TOrder tOrder);

    /**
     * 删除订单主
     * 
     * @param id 订单主主键
     * @return 结果
     */
    public int deleteTOrderById(Long id);

    /**
     * 批量删除订单主
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteTOrderByIds(Long[] ids);

    @Select("<script>"+
    " SELECT " +
            " o.id AS id, " +
            " o.order_uuid AS orderUuid, " +
            " o.order_code AS orderCode, " +
            " c.customer_name AS customerName, " +
            " c.customer_phone AS customerPhone, " +
            " c.customer_wechat AS customerWechat, " +
            " p.product_name AS productName, " +
            " op.vin AS vin, " +
            " op.vehicle_brand AS vehicleBrand, " +
            " f.file_url AS uploadFileUrl, " +
            " op.queryreport_url AS queryreportUrl, " +
            " o.total_price AS totalPrice, " +
            " o.total_purchasing_price AS totalPurchasingPrice, " +
            " o.total_real_price AS totalRealPrice, " +
            " o.total_return_price AS totalReturnPrice, " +
            " o.unread_flag as unreadFlag, " +
            " o.is_return_price as isReturnPrice, " +
            " op.order_product_data_uuid as orderProductDataUuid, " +
            " o.pay_datetime AS buyDatetime, " +
            " o.order_status AS orderStatus, " +
            " o.pay_status AS payStatus, " +
            " o.order_type AS orderType, " +
            " pc.customer_name AS agentName, " +
            " o.agency_fee AS agencyFee,  " +
            " o.transaction_id AS transactionId  " +
            "FROM " +
            " t_order o " +
            " LEFT JOIN t_customer c ON o.customer_uuid_mapping = c.customer_uuid " +
            " LEFT JOIN t_customer pc ON c.parent_customer_uuid = pc.customer_uuid " +
            " LEFT JOIN t_order_product_data op ON o.order_uuid = op.order_uuid_mapping " +
            " LEFT JOIN t_product p ON op.product_uuid_mapping = p.product_uuid " +
            " LEFT JOIN sys_file_data f ON op.upload_file_main_uuid = f.file_main_uuid"+
            "<where>" +
            "   o.pay_status='paid'   "+
            "   <if test='queryParams.orderType != null and queryParams.orderType != \"\"'> AND o.order_type=#{queryParams.orderType} </if>" +
            "   <if test='queryParams.orderCode != null and queryParams.orderCode != \"\"'> AND o.order_code=#{queryParams.orderCode} </if>" +
            "   <if test='queryParams.customerPhone != null and queryParams.customerPhone != \"\"'> AND c.customer_phone =#{queryParams.customerPhone} </if>" +
            "   <if test='queryParams.customerWechat != null and queryParams.customerWechat != \"\"'> AND c.customer_wechat =#{queryParams.customerWechat} </if>" +
            "   <if test='queryParams.orderStatus != null and queryParams.orderStatus != \"\"'> AND o.order_status=#{queryParams.orderStatus} </if>" +
            "   <if test='queryParams.customerName != null and queryParams.customerName != \"\"'> AND c.customer_name LIKE CONCAT('%',#{queryParams.customerName},'%') </if>" +
            "   <if test='queryParams.productName != null and queryParams.productName != \"\"'> AND p.product_name =#{queryParams.productName} </if>" +
            "   <if test='queryParams.vin != null and queryParams.vin != \"\"'> AND op.vin =#{queryParams.vin} </if>" +
            "   <if test='queryParams.vehicleBrand != null and queryParams.vehicleBrand != \"\"'> AND op.vehicle_brand =#{queryParams.vehicleBrand} </if>" +
            "   <if test='queryParams.agentCustomerName != null and queryParams.agentCustomerName != \"\"'> AND pc.customer_name LIKE CONCAT('%',#{queryParams.agentCustomerName},'%') </if>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (o.delete_flag = #{queryParams.deleteFlag} " +
            "       AND c.delete_flag = #{queryParams.deleteFlag}  " +
            "       AND p.delete_flag = #{queryParams.deleteFlag} " +
            "       AND f.delete_flag = #{queryParams.deleteFlag} " +
            "       AND op.delete_flag = #{queryParams.deleteFlag} ) " +
            "   </if>" +
            "   <if test='queryParams.buyStartTime != null and queryParams.buyEndTime != null'> AND o.pay_datetime &gt;= #{queryParams.buyStartTime} AND o.pay_datetime &lt;= #{queryParams.buyEndTime} </if>" +
            "</where>"+
            " order by o.id desc "+
            "</script>")
    public IPage<OrderQueryListPo> orderListQueryPage(IPage<OrderQueryListPo> page, @Param("queryParams") OrderListQueryMoReq  orderListQueryMoReq);

    @Select("<script>"+
            " SELECT " +
            " o.id AS id, " +
            " o.order_code AS orderCode, " +
            " p.product_name AS productName, " +
            " o.pay_datetime AS buyDatetime, " +
            " o.order_status AS orderStatus, " +
            " o.agency_fee AS agencyFee  " +
            "FROM " +
            " t_order o " +
            " LEFT JOIN t_customer c ON o.customer_uuid_mapping = c.customer_uuid " +
            " LEFT JOIN t_customer pc ON c.parent_customer_uuid = pc.customer_uuid " +
            " LEFT JOIN t_order_product_data op ON o.order_uuid = op.order_uuid_mapping " +
            " LEFT JOIN t_product p ON op.product_uuid_mapping = p.product_uuid " +
            "<where>" +
          "   <if test='queryParams.proxyCustomerUuid != null and queryParams.proxyCustomerUuid != \"\"'> AND pc.customer_uuid=#{queryParams.proxyCustomerUuid} </if>" +

            "   <if test='queryParams.buyStartTime != null and queryParams.buyEndTime != null'> AND o.pay_datetime &gt;= #{queryParams.buyStartTime} AND o.pay_datetime &lt;= #{queryParams.buyEndTime} </if>" +

            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (o.delete_flag = #{queryParams.deleteFlag} " +
            "       AND c.delete_flag = #{queryParams.deleteFlag}  " +
            "       AND p.delete_flag = #{queryParams.deleteFlag} " +
            "       AND pc.delete_flag = #{queryParams.deleteFlag} " +
            "       AND op.delete_flag = #{queryParams.deleteFlag} ) " +
            "   </if>" +
            "  <choose> "+
            "  <when test='queryParams.orderStatus != null and queryParams.orderStatus != \"\" and  queryParams.orderStatus==\"invalid\"   '>  " +
            "        and o.order_status =#{queryParams.orderStatus} </when>  "+
            "  <when test='queryParams.orderStatus != null and queryParams.orderStatus != \"\" and  queryParams.orderStatus==\"vaild\"'>  " +
            "       and o.order_status !='invalid' </when>  "+
            " <otherwise>  </otherwise>"+
            "  </choose> "+
            " and o.order_status !='init' "+
            "</where>"+
            " order by o.id desc "+
            "</script>")
    public List<OrderQueryProxyListPo> orderListProxyQuery(@Param("queryParams") OrderListProxyQueryMoReq orderListProxyQueryMoReq);





    @Select("<script>"+
            " SELECT " +
            " o.id AS id, " +
            " o.order_uuid AS orderUuid, " +
            " o.order_code AS orderCode, " +
            " c.customer_name AS customerName, " +
            " c.customer_phone AS customerPhone, " +
            " c.customer_wechat AS customerWechat, " +
            " p.product_type AS productType, " +
            " p.product_name AS productName, " +
            " op.vin AS vin, " +
            " op.vehicle_brand AS vehicleBrand, " +
            " f.file_url AS uploadFileUrl, " +
            " op.queryreport_url AS queryreportUrl, " +
            " o.total_price AS totalPrice, " +
            " o.total_purchasing_price AS totalPurchasingPrice, " +
            " o.total_real_price AS totalRealPrice, " +
            " o.total_return_price AS totalReturnPrice, " +
            " o.unread_flag as unreadFlag, " +
            " o.is_return_price as isReturnPrice, " +
            " o.pay_datetime AS buyDatetime, " +
            " o.order_status AS orderStatus, " +
            " o.pay_status AS payStatus, " +
            " o.order_type AS orderType, " +
            " pc.customer_name AS agentName, " +
            " o.agency_fee AS agencyFee,  " +
            " o.transaction_id AS transactionId  " +
            "FROM " +
            " t_order o " +
            " LEFT JOIN t_customer c ON o.customer_uuid_mapping = c.customer_uuid " +
            " LEFT JOIN t_customer pc ON c.parent_customer_uuid = pc.customer_uuid " +
            " LEFT JOIN t_order_product_data op ON o.order_uuid = op.order_uuid_mapping " +
            " LEFT JOIN t_product p ON op.product_uuid_mapping = p.product_uuid " +
            " LEFT JOIN sys_file_data f ON op.upload_file_main_uuid = f.file_main_uuid"+
            "<where>" +
            "   <if test='queryParams.id != null '> AND o.id=#{queryParams.id} </if>" +
            "   <if test='queryParams.orderUuid != null and queryParams.orderUuid != \"\"'> AND o.order_uuid=#{queryParams.orderUuid} </if>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (o.delete_flag = #{queryParams.deleteFlag} " +
            "       AND c.delete_flag = #{queryParams.deleteFlag}  " +
            "       AND p.delete_flag = #{queryParams.deleteFlag} " +
            "       AND f.delete_flag = #{queryParams.deleteFlag} " +
            "       AND op.delete_flag = #{queryParams.deleteFlag} ) " +
            "   </if>" +
             "</where>"+
            " order by o.id desc "+
            "</script>")
    public List<OrderDetailQueryPo> orderDetailQuery( @Param("queryParams") OrderDetailQueryMoReq orderDetailQueryMoReq);

    @Select("<script>"+
          "SELECT  " +
            "   t.orderMonth,  " +
            "   IFNULL(count(t.orderid),0) as orderNumber,  " +
            "   IFNULL(sum(t.total_real_price),0) as totolIncome,  " +
            "   IFNULL(sum(t.total_purchasing_price),0) as totolDisburse,  " +
            "   IFNULL(sum(t.agency_fee),0) as totalAgencyFee,  " +
            "   IFNULL(sum(t.gift_money),0) as totalGiftMoney  " +
            "FROM  " +
            "   (  " +
            "   SELECT  " +
            "      DATE_FORMAT(o.pay_datetime, '%Y-%m' ) AS orderMonth,  " +
            "      o.id as orderid,  " +
            "      o.total_real_price,  " +
            "      o.total_purchasing_price,  " +
            "      o.agency_fee,  " +
            "      c.gift_money   " +
            "   FROM  " +
            "      t_order o  " +
            "      LEFT JOIN t_order_product_data op ON o.order_uuid = op.order_uuid_mapping  " +
            "     LEFT JOIN t_customer c ON o.customer_uuid_mapping = c.customer_uuid   " +
            "<where>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (o.delete_flag = #{queryParams.deleteFlag} " +
            "       AND c.delete_flag = #{queryParams.deleteFlag} " +
              "      AND op.delete_flag = #{queryParams.deleteFlag} ) " +
            "   </if>" +
            "   <if test='queryParams.countBeginDate != null and queryParams.countEndDate != null'> AND o.pay_datetime &gt;= #{queryParams.countBeginDate} AND o.pay_datetime &lt;= #{queryParams.countEndDate} </if>" +

            "</where>"+
            "   ) t GROUP BY t.orderMonth " +
            "</script>")
    public List<OrderTotalCountDataBo> orderTotalCount(@Param("queryParams") TotalCountBoReq totalCountBoReq);

    @Select("<script>"+
            "SELECT   " +
            "   p.product_type AS productType,   " +
            "   IFNULL( count( o.id ), 0 ) AS orderNumber,   " +
            "   IFNULL( sum( o.total_real_price ), 0 ) AS totolIncome,   " +
            "   IFNULL( sum( o.total_purchasing_price ), 0 ) AS totolDisburse,   " +
            "   IFNULL( sum( o.agency_fee ), 0 ) AS totalAgencyFee,   " +
            "   IFNULL( sum( c.gift_money ), 0 ) AS totalGiftMoney    " +
            " FROM   " +
            "   t_order o   " +
            "   LEFT JOIN t_order_product_data op ON o.order_uuid = op.order_uuid_mapping   " +
            "   LEFT JOIN t_product p ON op.product_uuid_mapping = p.product_uuid   " +
            "   LEFT JOIN t_customer c ON o.customer_uuid_mapping = c.customer_uuid    " +
            "<where>" +
              "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (o.delete_flag = #{queryParams.deleteFlag} " +
            "       AND c.delete_flag = #{queryParams.deleteFlag}  " +
            "       AND p.delete_flag = #{queryParams.deleteFlag} " +
            "       AND op.delete_flag = #{queryParams.deleteFlag} ) " +
            "   </if>" +
            "   <if test='queryParams.countBeginDate != null and queryParams.countEndDate != null'> AND o.pay_datetime &gt;= #{queryParams.countBeginDate} AND o.pay_datetime &lt;= #{queryParams.countEndDate} </if>" +
            "</where>"+
            " GROUP BY p.product_type "+
            "</script>")
    public List<ProductTotalCountDataBo> productTotalCount(@Param("queryParams") TotalCountBoReq totalCountBoReq);



    @Select("<script>"+
            " SELECT " +
            " o.id AS id, " +
            " o.order_uuid AS orderUuid, " +
            " op.queryreport_url AS queryreportUrl, " +
            " op.queryreport_order_id AS queryreportOrderId " +
            "FROM " +
            " t_order o " +
            " LEFT JOIN t_order_product_data op ON o.order_uuid = op.order_uuid_mapping " +
            "<where>" +
            "   <if test='queryParams.orderUuid != null and queryParams.orderUuid != \"\"'> AND o.order_uuid=#{queryParams.orderUuid} </if>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (o.delete_flag = #{queryParams.deleteFlag} " +
            "       AND op.delete_flag = #{queryParams.deleteFlag} ) " +
            "   </if>" +
            "</where>"+
            "</script>")
    public List<OrderDetailQueryPo> orderDetailQuery2( @Param("queryParams") OrderDetailQueryMoReq orderDetailQueryMoReq);




    @Select("<script>" +
            " SELECT  " +
            " o.id as id, " +
            " o.sign_file_main_uuid as signFileMainUuid " +

            "FROM " +
            " t_order o " +
            "<where>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND o.delete_flag = #{queryParams.deleteFlag} " +
            "   </if>" +
            "   <if test='queryParams.orderCode != null and queryParams.orderCode != \"\"'> AND o.order_code =#{queryParams.orderCode} </if>" +
            "</where>" +
            "</script>")
    public List<OrderSignQueryPo> orderSignQuery(@Param("queryParams") OrderSignQueryMoReq orderSignQueryMoReq);


    public int deleteTOrderByUuids(@Param("deleteFlag") Long deleteFlag,@Param("orderUuids") String[] orderUuids);

    public int deleteTOrderByids(@Param("deleteFlag") Long deleteFlag,@Param("ids") Long[] ids);



}
